package com.person.jc.service.base.device;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.SQLSpell;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdDeviceRelationService extends Service {
	
	public final static BdDeviceRelationService me = new BdDeviceRelationService();
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from\n");
		sql.append("(select t.kind_code, t.kind_name, t1.device_id, t1.device_name from ts_code_kind_data t \n");
		sql.append("left join bd_device_info t1 on t.kind_code = t1.device_type and t1.if_valid = 1\n");
		sql.append("where t.type_code = 'device_type'\n");
		sql.append(") a left join bd_device_relation b on  a.kind_code = b.device_type and a.device_id = b.device_id_");
		
		
		/*
		sql.append("SELECT t.*, t1.t_name record0Name, ("+SQLSpell.kindName("device_type", "t.device_type")+") device_type_name \n");
		sql.append("  FROM bd_device_info t\n");
		sql.append("  LEFT JOIN ts_template_info t1\n");
		sql.append("    ON t.record_0 = t1.t_id\n");
		sql.append(" WHERE t.if_valid = 1");*/
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceName")))){
			sql.append(" and t.device_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceType")))){
			sql.append(" and t.device_type = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceId")))){
			sql.append(" and t.device_id = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("gasType")))){
			sql.append(" and t.device_id in ("+ temp +")");
		}
//		sql.append(" order by t.device_type, t.device_name ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		String sql = "insert into bd_device_info (device_name, device_type, remark, record_0) values (?, ?, ?, ?)";
		Db.update(sql, map.get("deviceName"), map.get("deviceType"), map.get("remark"), map.get("record0"));
		super.setCRUD("仪器名称维护", "C", "新增仪器信息，名称："+map.get("deviceName"));
	}
	
	public void update(Map<String,Object> map){
		String sql = " update bd_device_info set device_name = ? , device_type = ?, remark = ?, record_0 =  ? where device_id = ?";
		Db.update(sql, map.get("deviceName"), map.get("deviceType"), map.get("remark"), map.get("record0"), map.get("deviceId"));
		super.setCRUD("仪器名称维护", "U", "修改仪器信息，仪器编号："+map.get("deviceId"));
	}
	
	public void delete(String deviceId){
		String sql = " update bd_device_info set if_valid = 0 where device_id = ? ";
		Db.update(sql, deviceId);
		super.setCRUD("仪器名称维护", "D", "删除仪器信息，仪器编号："+deviceId);
	}

	public void delConfigureItemData(Integer scopeId){
		String delSql="delete from bd_device_relation where scope_id=?";
		Db.update(delSql,scopeId);
	}

	public void saveConfigureItemData(Map<String,Object> map){



		Record record = new Record();
		record.set("device_type", map.get("deviceType"));
		record.set("device_id_", map.get("deviceId"));
		record.set("scope_id", map.get("scopeId"));
		record.set("create_date", new Date());
		record.set("sort", map.get("sort"));
		Db.save("bd_device_relation", record);
	}

	public List<Record> getConfigureItemDataByScopeId(Integer scopeId){
		String sql="select id, device_type, device_id_, sort from bd_device_relation where scope_id=? order by device_type , sort";
		return Db.find(sql,scopeId);
	}
	
}
